
$(function() {

    initSwitch("rememberMe", false);

    $('#loginForm').validate({
        rules:{
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: "用户名/邮箱不能为空！"
            },
            password: {
                required: "密码不能为空！"
            }
        },

        submitHandler: function(form){
            waitMessage();
            $.ajax({
                    type: "post",
                    url: path+"/login",
                    data: {
                        username: $('#username').val(),
                        password: $('#password').val(),
                        rememberMe: $('#rememberMe').val()
                    },
                    //服务器返回数据类型。如果不指定，将自动根据 HTTP 包 MIME 信息来判断
                    dataType: 'json',
                    success: function(data, textStatus){
                        if(data.success){
                            window.location = (path + data.msg);
                        } else if(data.msg.indexOf("激活")){
                            //未激活则有发送激活邮件的按钮
                            swAjax({
                                title: "账号未激活",
                                text: "",
                                confirmButtonText: "发送激活邮件",
                                cancelButtonText: "确定",
                                callback: function(){
                                    $.ajax({
                                        url: path + "/sendActiveMail",
                                        dataType: "json",
                                        type: "post",
                                        data: {
                                            email: $("#username").val()
                                        },
                                        success: function(data){
                                            if(data.success){
                                                successMessage("激活邮件发送成功！");
                                            } else{
                                                errorMessage("激活邮件发送失败，请重试！");
                                            }
                                        },
                                        error: function(XMLHttpRequest, textStatus, errorThrown){
                                            errorMessage("激活邮件发送失败，错误代码： " + XMLHttpRequest.status);
                                        }
                                    });
                                }
                            });
                        }
                        else errorMessage(data.msg);
                    },
                    // 通常 textStatus 和 errorThrown 之中只有一个会包含信息
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        //this：调用本次AJAX请求时传递的options参数
                        errorMessage("登录失败，错误代码： " + XMLHttpRequest.status);
                    }
                }
            );
        }
    });
});